package com.faeple.admin.service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.faeple.admin.dao.AdminDao;
import com.faeple.common.entity.NoticeDto;
import com.faeple.common.entity.PageDto;
import com.faeple.log.entity.LogHackDto;
import com.faeple.log.entity.LogLoginLogoutDto;
import com.faeple.notifications.dao.NotificationsDao;
import com.faeple.post.entity.CommentDto;
import com.faeple.post.entity.PostDto;
import com.faeple.user.entity.UserDto;
import com.google.android.gcm.server.Message;
import com.google.android.gcm.server.Result;
import com.google.android.gcm.server.Sender;

@Service
public class AdminServiceImpl implements AdminService {

	@Autowired
	private AdminDao dao;
	@Autowired
	private NotificationsDao notiDao;
	
	@Override
	public List<UserDto> getTodayLoginMember() {
		// TODO Auto-generated method stub
		return dao.getTodayLoginMember();
	}

	@Override
	public List<UserDto> getTodaySignupMember() {
		// TODO Auto-generated method stub
		return dao.getTodaySignupMember();
	}

	@Override
	public List<HashMap<String, String>> getTodayLeaveMember() {
		// TODO Auto-generated method stub
		return dao.getTodayLeaveMember();
	}

	@Override
	public List<PostDto> getTodayUploadPost() {
		// TODO Auto-generated method stub
		return dao.getTodayUploadPost();
	}

	@Override
	public List<CommentDto> getTodayAddComment() {
		// TODO Auto-generated method stub
		return dao.getTodayAddComment();
	}

	@Override
	public List<HashMap<String, String>> getTodayAddTag() {
		// TODO Auto-generated method stub
		return dao.getTodayAddTag();
	}

	@Override
	public int getTotalMemberCount() {
		// TODO Auto-generated method stub
		return dao.getTotalMemberCount();
	}

	@Override
	public int getTotalNoticeCount() {
		return dao.getTotalNoticeCount();
	}

	@Override
	public PageDto getPageStatus(int pageno, int loadSize, int totalCount) {
		PageDto page = new PageDto();
		page.setTotal_record(totalCount);
		page.setPage_per_record_cnt(loadSize);
		page.setPageno(pageno);
		
		page.setRecord_end_no(page.getPageno()*page.getPage_per_record_cnt());
		page.setRecord_start_no(page.getRecord_end_no()-(page.getPage_per_record_cnt()-1));
		if(page.getRecord_end_no()>page.getTotal_record()){
			page.setRecord_end_no(page.getTotal_record());
		}
		
		page.setTotal_page(page.getTotal_record() / page.getPage_per_record_cnt() + (page.getTotal_record() % page.getPage_per_record_cnt() > 0 ? 1: 0));
		if(page.getPageno() > page.getTotal_page()){
			page.setPageno(page.getTotal_page());
		}
		
		page.setGroup_no(page.getPageno()/page.getGroup_per_page_cnt()+(page.getPageno()%page.getGroup_per_page_cnt()>0 ? 1:0));
		
		page.setPage_eno(page.getGroup_no()*page.getGroup_per_page_cnt());
		
		page.setPage_sno(page.getPage_eno()-(page.getGroup_per_page_cnt()-1));
		if(page.getPage_eno()>page.getTotal_page()){
			page.setPage_eno(page.getTotal_page());
		}
		
		page.setPrev_pageno(page.getPage_sno()-page.getGroup_per_page_cnt());
		page.setNext_pageno(page.getPage_sno()+page.getGroup_per_page_cnt());
		
		if(page.getPrev_pageno()<1){
			page.setPrev_pageno(1);
		}
		if(page.getNext_pageno()>page.getTotal_page()){
			page.setNext_pageno(page.getTotal_page()/page.getGroup_per_page_cnt()*page.getGroup_per_page_cnt()+1);
			page.setNext_pageno(page.getTotal_page());
		}
		return page;
	}

	@Override
	public List<UserDto> getMemberList(HashMap<String, Object> param) {
		return dao.getMemberList(param);
	}

	@Override
	public List<PostDto> getPostList(HashMap<String, Object> param) {
		return dao.getPostList(param);
	}

	@Override
	public List<LogLoginLogoutDto> getLogLogin(HashMap<String, Object> param) {
		return dao.getLogLogin(param);
	}

	@Override
	public List<LogHackDto> getLogHackList(HashMap<String, Object> param) {
		return dao.getLogHackList(param);
	}

	@Override
	public int sendGcmPush(HashMap<String, String> param) {
		if(param.get("target").equals("user")){
			String reg_id = notiDao.checkUseAndroid(param.get("target-idx"));
			if(reg_id != null && !reg_id.equals("")){
				Sender sender = new Sender("AIzaSyCBYHunMiiz09btDbOfM5KCH4CC_OEO5Ts");
				Message msg;
				msg = new Message.Builder().addData("title", param.get("title")).addData("message", param.get("content")).build();
				try {
					Result result = sender.send(msg, reg_id, 5);
					return 1;
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return 0;
				}
			}else{
				return 2;
			}
		}else{
			int errorCnt = 0;
			List<String> reg_id_list = dao.getGcmRegidAll();
			for (String reg_id : reg_id_list) {
				if(reg_id != null && !reg_id.equals("")){
					Sender sender = new Sender("AIzaSyCBYHunMiiz09btDbOfM5KCH4CC_OEO5Ts");
					Message msg;
					msg = new Message.Builder().addData("title", param.get("title")).addData("message", param.get("content")).build();
					try {
						Result result = sender.send(msg, reg_id, 5);
						
					} catch (IOException e) {
						errorCnt++;
					}
				}else{
					errorCnt++;
				}
			}
			return errorCnt;
		}
	}

	@Override
	public UserDto getMember(String idx) {
		return dao.getMember(idx);
	}

}
